<?php

namespace App\Member\Metrics\Examples;

use Dcat\Admin\Widgets\Metrics\Card;
use Illuminate\Http\Request;
use Dcat\Admin\Admin;
use App\Models\Deposite;



class TotalUsers extends Card
{

    /**
     * 初始化卡片内容
     *
     * @return void
     */
    protected function init()
    {
        parent::init();

        $this->height = 130;
        $this->title('投入数量');
        $this->user = Admin::user();
//        $this->dropdown([
//            '7' => 'Last 7 Days',
//            '28' => 'Last 28 Days',
//            '30' => 'Last Month',
//            '365' => 'Last Year',
//        ]);
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        $generator = function ($len, $min = 10, $max = 300) {
            for ($i = 0; $i <= $len; $i++) {
                yield mt_rand($min, $max);
            }
        };

        switch ($request->get('option')) {
            case '28':
                // 卡片内容
                $this->withContent(mt_rand(400, 1000).'k');
                // 图表数据
                break;
            case '7':
            default:
                // 卡片内容
                $this->withContent(getNumberFormat($this->user->investment_amount));

        }
    }



    /**
     * 设置卡片内容.
     *
     * @param string $content
     *
     * @return $this
     */
    public function withContent($content)
    {
        return $this->content(
            <<<HTML
            <!--<style>-->
                <!--#app .row .col-md-4 :nth-child(1){-->
                    <!--background-image: url("image/1.png");-->
                    <!---->
                <!--}-->
                <!--#app .row .col-md-4 h4 {color: #ffffff;}-->
                <!--#app .row .col-md-4 h1 {color: #ffffff;}-->
                <!---->
                <!--#app .row .col-md-4 .justify-content-between div{-->
                    <!--background-image: url("xxx.jpg")!important;-->
                <!--}-->
                <!--#app .row .col-md-4 .justify-content-between div h4{-->
                    <!--background-image: url("xxx.jpg")!important;-->
                <!--}-->
                <!--#app .row .col-md-4 .justify-content-between div h1{-->
                    <!--background-image: url("xxx.jpg")!important;-->
                <!--}-->
            <!--</style>-->

<div class="d-flex justify-content-between align-items-center mt-1">
    <div class="text-left">
        <h1 class="ml-1 font-lg-1">{$content}$</h1>
    </div>
</div>
HTML
        );
    }


    public function grow (){
        $origin = Deposite::where('user_id', $this->user->id)
            ->whereBetween('created_at', [getFirstOfMonth(), getLastOfMonth()])
            ->select([
                \DB::raw('SUM(usd_amount) as usd_amount'),
                \DB::raw('DATE_FORMAT(created_at,"%Y-%m") as date'),
            ])->groupBy('date')->first()->usd_amount;

        $last_month = Deposite::where('user_id', $this->user->id)
            ->whereBetween('created_at', [getFirstOfMonth(1), getLastOfMonth(1)])
            ->select([
                \DB::raw('SUM(usd_amount) as usd_amount'),
                \DB::raw('DATE_FORMAT(created_at,"%Y-%m") as date'),
            ])->groupBy('date')->first()->usd_amount;

        $grow = round(($origin - $last_month)/$last_month * 100);

        return $grow;
    }
}
